#include <cjsonarray.h>
#include <cjsonvalue_common.h>

CJsonArray::CJsonArray() :
  CJsonValue(JV_ARRAY)
{
}

CJsonArray::CJsonArray(const CJsonArray * value) :
  CJsonValue(JV_ARRAY)
{
  value->GetValue(this->members);
}

CJsonArray::~CJsonArray()
{
  while (!members.empty())
  {
    if (members.back())
    {
      delete members.back();
    }
    members.pop_back();
  }
}

jstring CJsonArray::ToString() const
{
  jstring result = J("[");
  for (unsigned i = 0; i < members.size(); i++)
  {
    if (i)
    {
      result += J(",");
    }
    result += members[i]->ToString();
  }
  result += J("]");
  return result;
}

void CJsonArray::AddMember(const jchar * member)
{
  if (member)
  {
    members.push_back(new CJsonValueString(member));
  }
  else
  {
    members.push_back(new CJsonValueNull());
  }
}

void CJsonArray::AddMember(jstring member)
{
  members.push_back(new CJsonValueString(member));
}

void CJsonArray::AddMember(jint member)
{
  members.push_back(new CJsonValueNumber(member));
}

void CJsonArray::AddMember(bool member)
{
  members.push_back(new CJsonValueBool(member));
}

void CJsonArray::AddMember(const CJsonValueString * member)
{
  members.push_back(new CJsonValueString(member)); 
}

void CJsonArray::AddMember(const CJsonValueNumber *member)
{
  members.push_back(new CJsonValueNumber(member)); 
}

void CJsonArray::AddMember(const CJsonValueBool *member)
{
  members.push_back(new CJsonValueBool(member)); 
}

void CJsonArray::AddMember(const CJsonValueNull *member)
{
  members.push_back(new CJsonValueNull()); 
}

void CJsonArray::AddMember(const CJsonObject *member)
{
  members.push_back(new CJsonObject(member)); 
}

void CJsonArray::AddMember(const CJsonArray *member)
{
  members.push_back(new CJsonArray(member)); 
}

void CJsonArray::AddMember(const CJsonValue * member)
{
  members.push_back(CopyValue(member));
}

const CJsonValue * CJsonArray::operator [] (unsigned index) const
{
  if (index >= members.size())
  {
    return NULL;
  }
  return members[index];
}

bool CJsonArray::Empty() const
{
  return members.empty();
}

std::size_t CJsonArray::Size() const
{
  return members.size();
}

void CJsonArray::GetValue(std::vector <CJsonValue*> & value) const
{
  for (unsigned i = 0; i < members.size(); i++)
  {
    value.push_back(CopyValue(members[i]));
  }
}

